/**
 * class name: iview,
 * copyright: maximus zhang,
 * License: MIT-LICENSE,
 * contact: maximus.zhang@gmail.com,
 * required-js: http://jsease.googlecode.com/svn/trunk/core.js,
 * update: http://jsease.googlecode.com/svn/trunk/iview.js
 * -------
 */
function iview() {
    var A = arguments,
        _ = this;
    _ = ext( _, ext(
        {
        id: 'iview',
        elem: me.body,
        cls: 'iview',
        type: 'tile',
        data:[
                {
                id: 'codev',
                title: 'name',
                img: '',
                label: 'spec',
                text: '',
                price: 0
                }
            ],
        pfx: {
            y0 : '',
            y1 : '',
            y2 : '',
            y3 : ''
            },
        fn:''
        }, A[0] ) );

    _.uitype = 'iview';

    _.elem = $(_.elem) || me.body;

    _.setPrice = function (P) {
    return P.price || '';
    };

    _.setSpec = function (P) {
    return P || '';
    };

    _.setCode = function (c) {
    return c || '';
    };

    _.setType = function (t) {
        if ( /tile|list/i.test(t) && t!=_.type ) {
            _.type = t;
            _.view.empty();
            _.add(_.data);
        }
    return _;
    };

    _.add = function (v) {
        if ( !isArray(v) ) {
            return _;
        }

        for ( var i=0, L=v.length; i<L; i++ ) {
            var div = elm('div').setClass('iview-'+ (/tile|list/i.test(_.type) ? _.type
                    + (/list/i.test(_.type) ? i%2 : '') : 'tile') ).appendTo(_.view);
            div.attr('id', v[i].id);
            div.onmouseover = function () {
                this.style.borderColor = '#45b035';
            return false;
            };
            div.onmouseout = function () {
                this.style.borderColor = '#cde';
            return false;
            };

            /* title */
            var tit = elm('div').setClass('iview-title').appendTo(div),
                titext = elm('span').setClass('title-text').appendTo(tit).html(v[i].title);
            if ( /tile/i.test(_.type) ) {
                var titbegin = elm('span').setClass('title-begin').before(titext),
                    titend = elm('span').setClass('title-end').appendTo(tit);
            }

            var tile = elm('div').setClass('iview-data').appendTo(div),
                tm = elm('div').setClass('data-main').appendTo(tile);

            if ( /tile/i.test(_.type) ) {
                var img = elm('img').setClass('data-img').attr('src', v[i].img)
                        .attr('alt', v[i].title).appendTo(tm);
            }

            var cod = elm('div').setClass('data-y0').appendTo(tm).html(_.setCode(v[i].id));

            if ( p(v[i], 'label') ) {
                var lbl = elm('div').setClass('data-y1').appendTo(tm).html(_.setSpec(v[i].label));
            }

            if ( hop(v[i], 'price') ) {
                var prc = elm('div').setClass('data-y2').appendTo(tm).html(_.setPrice(v[i]));
            }

            var buy = elm('div').setClass('data-y3').appendTo(tm),
                cart = elm('button').setClass('buy-cart').attr('type', 'button').attr('value',v[i].id).appendTo(buy),
                ico = elm('img').setClass('cart-ico').attr('src', '/icos/cart_add.png').appendTo(cart),
                cartxt = elm('span').setClass('cart-text').appendTo(cart).html('订购');

            if ( typeof _.fn == 'function' ) {
                cart.onclick = function () {
                return _.fn.apply(this, [cart]);
                };
            } else if ( typeof _.fn == 'string' ) {
                cart.attr('onclick', _.fn);
            }

            /* if ( p(v[i], 'text') ) {
                var txt = elm('div').setClass('data-detail').appendTo(tile);
                //txt.innerHTML = v[i].text;
            } */

        }

    return _;
    };

    _.init = function () {
        _.view = elm('div').setClass(_.cls).attr('id', _.id).appendTo(_.elem);
        _.add(_.data);
    return _;
    };

}